package com.suray.wcs.service.plc;

import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.service.enums.NotificationTypeEnum;
import com.suray.wcs.service.init.SystemInit;
import com.suray.wcs.service.system.entity.PortDB;
import com.suray.wcs.service.system.service.IWareHouseDBService;
import com.suray.wcs.service.system.service.impl.WareHouseDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;
import com.suray.wcs.service.wmsconnect.ToWMSImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BarCodeScanner {

    @Autowired
    private static IWareHouseDBService wareHouseDBService = SpringContextHolder.getBean(WareHouseDBServiceImpl.class);
    private static ToWMSImpl toWMS = SpringContextHolder.getBean(ToWMSImpl.class);
    private static Logger log = LoggerFactory.getLogger(BarCodeScanner.class);

    public void start(PortDB port) {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleWithFixedDelay(() -> {
            Thread.currentThread().setName(port.getPortName() + "扫码外形称重监控线程");
            PortDB realPort = SystemInit.PORTS.get(port.getPortId());
            try {
                if (realPort != null) {
                    if (realPort.getStations() != null) {
                        SingleBarCodeScanner.start(realPort);
                    } else {
                        NormalBarCodeScanner.start(realPort);
                    }
                }
            } catch (Exception e) {
                WcsFuncUtil.exceptionLog(e, this.getClass());
                log.error(realPort.getPortName() + " 扫码异常", e);
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);
        log.info("{}扫码轮询线程启动", port.getPortName());
    }
}
